# Maintainer: zxp19821005 <zxp19821005 at 163 dot com>
# Contributor: Felix Golatofski <contact@xdfr.de>
# Contributor: Fancy <springzfx at gmail.com>
pkgname=lx-music-desktop
_pkgname='LX Music'
_zhsname='洛雪音乐助手'
pkgver=2.12.0
_electronversion=37
_nodeversion=22
pkgrel=99
pkgdesc="A music software based on Electron + Vue.(Use system-wide electron)一个基于 Electron + Vue 开发的音乐软件"
arch=(
    'aarch64'
    'armv7h'
    'x86_64'
)
url="https://lyswhut.github.io/lx-music-doc/"
_ghurl="https://github.com/lyswhut/lx-music-desktop"
license=(
    'Apache-2.0'
    'LicenseRef-custom'
)
depends=(
    "electron${_electronversion}"
)
makedepends=(
    'npm'
    'nvm'
    'git'
    'curl'
    'gendesk'
)
source=(
    "${pkgname}-${pkgver}::git+${_ghurl}#tag=v${pkgver}"
    "${pkgname}.sh"
)
sha256sums=('39ea73306bb6e718746acaec48772918e7de91e22705e5f60b10387fa619366c'
            '31ad33b633744f5361abd964be306cea53ae1050e760c787115f7eca60045ae6')
_ensure_local_nvm() {
    local NVM_DIR="${srcdir}/.nvm"
    source /usr/share/nvm/init-nvm.sh || [[ $? != 1 ]]
    nvm install "${_nodeversion}"
    nvm use "${_nodeversion}"
}
_get_electron_version() {
    _elec_ver="$(grep -m 1 '"electron":' "${srcdir}/${pkgname}-${pkgver}/package.json" | cut -d'"' -f4 | tr -d '^' | cut -d. -f1)"
    echo -e "The electron version is: \033[1;31m${_elec_ver}\033[0m"
}
prepare() {
    cd "${srcdir}/${pkgname}-${pkgver}"
    _get_electron_version
    sed -i -e "
        s/@electronversion@/${_electronversion}/g
        s/@appname@/${pkgname}/g
        s/@runname@/app.asar/g
        s/@cfgdirname@/${_pkgname}/g
        s/@options@/env ELECTRON_OZONE_PLATFORM_HINT=auto/g
    " "${srcdir}/${pkgname}.sh"
    gendesk -q -f -n \
        --pkgname="${pkgname}" \
        --pkgdesc="${pkgdesc}" \
        --categories="AudioVideo" \
        --name="${_pkgname}" \
        --exec="${pkgname} %U"
    sed -i "5i\Name[zh_CN]=${_zhsname}" "${srcdir}/${pkgname}-${pkgver}/${pkgname}.desktop"
    export ELECTRON_SKIP_BINARY_DOWNLOAD=1
    export SYSTEM_ELECTRON_VERSION="$(electron${_electronversion} -v | sed 's/v//g')"
    HOME="${srcdir}/.electron-gyp"
    {
        echo -e '\n'
        #echo 'build_from_source=true'
        echo "cache=${srcdir}/.npm_cache"
    } >> .npmrc
    if [[ "$(curl -s ipinfo.io/country)" == *"CN"* ]]; then
        {
            echo 'registry=https://registry.npmmirror.com'
            echo 'electron_mirror=https://registry.npmmirror.com/-/binary/electron/'
            echo 'electron_builder_binaries_mirror=https://registry.npmmirror.com/-/binary/electron-builder-binaries/'
        } >> .npmrc
        find ./ -type f -name "package-lock.json" -exec sed -i "s/registry.npmjs.org/registry.npmmirror.com/g" {} +
    fi
    _ensure_local_nvm
    sed -i "s/\"electron\": \"[^\"]*\"/\"electron\": \"${SYSTEM_ELECTRON_VERSION}\"/g" package.json
    # Use system-wide electron to pack
    sed -i -e "
        6i\const electronDist = process.env.ELECTRON_DIST || '/usr/lib/electron'
        26i\  electronDist: electronDist,
        26i\  electronDownload: {
        26i\    mirror: false,
        26i\  },
    " build-config/build-pack.js
    NODE_ENV=development    npm install
}
build() {
    cd  "${srcdir}/${pkgname}-${pkgver}"
    _ensure_local_nvm
    export electronDist="/usr/lib/electron${_electronversion}"
    NODE_ENV=production     npm run pack:dir
}
package() {
    install -Dm755 "${srcdir}/${pkgname}.sh" "${pkgdir}/usr/bin/${pkgname}"
    install -Dm644 "${srcdir}/${pkgname}-${pkgver}/build/linux-"*/resources/app.asar -t "${pkgdir}/usr/lib/${pkgname}"
    cp -Pr --no-preserve=ownership "${srcdir}/${pkgname}-${pkgver}/build/linux-"*/resources/licenses "${pkgdir}/usr/lib/${pkgname}"
    _icon_sizes=(16x16 32x32 48x48 64x64 128x128 256x256 512x512)
    for _icons in "${_icon_sizes[@]}";do
        install -Dm644 "${srcdir}/${pkgname}-${pkgver}/resources/icons/${_icons}.png" \
            "${pkgdir}/usr/share/icons/hicolor/${_icons}/apps/${pkgname}.png"
    done
    install -Dm644 "${srcdir}/${pkgname}-${pkgver}/${pkgname}.desktop" -t "${pkgdir}/usr/share/applications"
    install -Dm644 "${srcdir}/${pkgname}-${pkgver}/licenses/license"* -t "${pkgdir}/usr/share/licenses/${pkgname}"
}